package org.roger.search.filter;

import org.apache.lucene.search.CachingWrapperFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.TermRangeFilter;

/**
 * 范围过滤器
 * @param field 搜索域
 * @param min 最小边界
 * @param max 最大边界
 * @param includeLower 是否包含最小边界
 * @param includeUpper 是否包含最大边界
 * @param isCache 是否启用域缓存
 * @author luoweiyi
 */
public class RangeFilter<T> implements LuceneSearchFilter{
	
	private String field;
	private T min;
	private T max;
	private boolean includeLower;
	private boolean includeUpper;
	private boolean isCache;
	
	public RangeFilter(String field,T min,T max,boolean includeLower, boolean includeUpper){
		this(field,min,max,includeLower,includeUpper,false);
	}
	
	public RangeFilter(String field,T min,T max,boolean includeLower, boolean includeUpper,boolean isCache){
		this.field = field;
		this.min = min;
		this.max = max;
		this.includeLower = includeLower;
		this.includeUpper = includeUpper;
		this.isCache = isCache;
	}
	
	@Override
	public Filter searchFilter(){
		Filter filter = null;
		
		if(min != null || max != null){
			if(min != null && max != null){
				if(min instanceof String){
					filter =  TermRangeFilter.newStringRange(field,(String)min,(String)max,includeLower,includeUpper);
				}else if(min instanceof Integer){
					filter =  NumericRangeFilter.newIntRange(field,(Integer)min,(Integer)max,includeLower,includeUpper);
				}else if(min instanceof Long){
					filter =  NumericRangeFilter.newLongRange(field,(Long)min,(Long)max,includeLower,includeUpper);
				}else if(min instanceof Float){
					filter =  NumericRangeFilter.newFloatRange(field,(Float)min,(Float)max,includeLower,includeUpper);
				}else if(min instanceof Double){
					filter =  NumericRangeFilter.newDoubleRange(field,(Double)min,(Double)max,includeLower,includeUpper);
				}
			}else if(min == null){
				if(max instanceof String){
					filter =  TermRangeFilter.newStringRange(field,null,(String)max,includeLower,includeUpper);
				}else if(max instanceof Integer){
					filter =  NumericRangeFilter.newIntRange(field,null,(Integer)max,includeLower,includeUpper);
				}else if(max instanceof Long){
					filter =  NumericRangeFilter.newLongRange(field,null,(Long)max,includeLower,includeUpper);
				}else if(max instanceof Float){
					filter =  NumericRangeFilter.newFloatRange(field,null,(Float)max,includeLower,includeUpper);
				}else if(max instanceof Double){
					filter =  NumericRangeFilter.newDoubleRange(field,null,(Double)max,includeLower,includeUpper);
				}
			}else{
				if(min instanceof String){
					filter =  TermRangeFilter.newStringRange(field,(String)min,null,includeLower,includeUpper);
				}else if(min instanceof Integer){
					filter =  NumericRangeFilter.newIntRange(field,(Integer)min,null,includeLower,includeUpper);
				}else if(min instanceof Long){
					filter =  NumericRangeFilter.newLongRange(field,(Long)min,null,includeLower,includeUpper);
				}else if(min instanceof Float){
					filter =  NumericRangeFilter.newFloatRange(field,(Float)min,null,includeLower,includeUpper);
				}else if(min instanceof Double){
					filter =  NumericRangeFilter.newDoubleRange(field,(Double)min,null,includeLower,includeUpper);
				}
			}
		}
		if(filter != null && isCache){
			filter = new CachingWrapperFilter(filter);
		}
		
		return filter;
	}
}
